//
//  ZCXCollectViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXCollectViewController.h"
#import "ZCXCollectTableViewCell.h"
#import "CoreDataManager.h"
#import "ZPetVideo.h"
#import "ZCXVideoDetialViewController.h"
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXCollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) CoreDataManager *coreDataManager;
@property (nonatomic, retain) NSMutableArray *collectArray;
@end

@implementation ZCXCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ZPetVideo"];
    NSArray *array = [self.coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
    self.collectArray = [NSMutableArray arrayWithArray:array];
    [self.tableView reloadData];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStylePlain];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //注册Cell
    [self.tableView registerClass:[ZCXCollectTableViewCell class] forCellReuseIdentifier:@"zcxcollecttableviewcell"];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.collectArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zcxcollecttableviewcell" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    cell.petVideo = self.collectArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)viewWillAppear:(BOOL)animated{
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ZPetVideo"];
    NSArray *array = [self.coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
    self.collectArray = [NSMutableArray arrayWithArray:array];
    [self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.delegate ZCXPushToVideo:self.collectArray[indexPath.row]];
    
}
#pragma mark 是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark 编辑的样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  UITableViewCellEditingStyleDelete;
}
#pragma mark 编辑的实现
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[self.collectArray[indexPath.row] videoID]];
    [self.coreDataManager.managedObjectContext deleteObject:self.collectArray[indexPath.row]];
    [self.collectArray removeObject:self.collectArray[indexPath.row]];
    [self.coreDataManager saveContext];
    [self.tableView reloadData];
    
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
#pragma mark 设置分割线顶头
-(void)viewDidLayoutSubviews

{
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
        
    }
    
    
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
        
    }
    
}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
